<template>
    <a-modal title="同步数据" :visible="visible" @cancel="visible = false" v-if="visible">
        <a-radio-group v-model:value="type">
            <a-radio :style="radioStyle" :value="1">将jira issue同步（覆盖）产品需求</a-radio>
            <a-radio :style="radioStyle" :value="2">将产品需求同步（覆盖）jira issue</a-radio>
        </a-radio-group>
        <template #footer>
            <flexbox justify="flex-end" class="btn-group">
                <a-button @click="visible = false">取消</a-button>
                <a-button type="primary" @click="handleSubmit">同步</a-button>
            </flexbox>
        </template>
    </a-modal>
</template>
<script lang="ts">
import api from '@/api';
import { useMessage } from '@/hooks/common/useMessage';
import { message } from 'ant-design-vue';
import { defineComponent, reactive, ref, unref, toRefs, h } from 'vue';
export default defineComponent({
    name: 'updateModal',
    props: {
        // 系统
        systemId: {
            type: [String, Number],
            default: ''
        }
    },
    setup(props) {
        const { createConfirm } = useMessage();
        const state = reactive<any>({
            type: 1,
            visible: false,
            detail: {},
            radioStyle: {
                display: 'block',
                height: '30px',
                lineHeight: '30px'
            }
        });
        const type = ref<number>(1);
        const handleSubmit = async () => {
            const request = unref(type) === 1 ? api.jiraModel.syncProductFromJira : api.jiraModel.syncIssueFromPmp;
            const { issueType, productId, issueKey } = state.detail;
            const query = {
                requireSystem: props.systemId,
                issueType,
                issueKey,
                id: productId
            };
            const { success, errorMsg, errorCode } = await request({
                ...query
            });
            if (success) {
                message.success('更新成功');
                state.visible = false;
            } else {
                if (errorCode === '004') {
                    const index = errorMsg.indexOf('http');
                    let url = '';
                    if (index >= 0) {
                        url = errorMsg.slice(index);
                    }
                    createConfirm({
                        title: h('div', [
                            h('p', errorMsg.slice(0, index - 1)),
                            h(
                                'a',
                                {
                                    attrs: {
                                        href: url
                                    }
                                },
                                url
                            )
                        ]),
                        iconType: 'danger',
                        okText: '确认跳转',
                        onOk: async () => {
                            window.open(url);
                        }
                    });
                    return;
                }
                message.error(errorMsg);
            }
        };
        const open = (detail) => {
            state.visible = true;
            state.detail = detail;
        };
        return {
            ...toRefs(state),
            type,
            handleSubmit,
            open
        };
    }
});
</script>
